package phone.book;

import model.Group;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import customlists.GroupListAdapter;
import database.GroupAdapter;


public class MainPageActivity extends ListActivity {
	
    private static final int ADD_CONTACT = 0;
	AlertDialog.Builder alert;
    GroupAdapter ga;
    ListView lv;
    ArrayList<Group> groupsList;
    private Context ctx;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.groupmain); 
        ctx=this;
        ga=new GroupAdapter(getApplicationContext());
		lv=getListView();
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			final int position=arg2;
			alert=new AlertDialog.Builder(ctx);
			alert.setTitle("Delete Group");
	    	alert.setMessage("Confrim delete group "+groupsList.get(arg2).getGroupName());
	    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					deleteGroup(position);
				}
			});
	    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
	    	alert.create().show();
			return true;
		}
	});
	refreshList();  
    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.groupmenu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case R.id.addGroup: addGroup();return true;
			
			default:return super.onOptionsItemSelected(item);
		}
	}
	
	public void addGroup(){
    	alert=new AlertDialog.Builder(this);
    	alert.setTitle("Create New Group");
    	alert.setMessage("Enter group name");
    	final EditText inputGrpName=new EditText(this);
    	alert.setView(inputGrpName);
    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				String grpName=inputGrpName.getText().toString();
				createNewGroup(grpName);
				refreshList();
			}
		});
    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
    	alert.create().show();
    }
	
    private void createNewGroup(String groupName) {
		Group g=new Group();
		g.setGroupName(groupName);
		ga.open();
		ga.createGroup(g);
		ga.close();
	}
   
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		refreshList();
		int groupId=groupsList.get(position).getId();
		String groupName=groupsList.get(position).getGroupName();
		Intent i=new Intent(getApplicationContext(),GroupOperations.class);
		i.putExtra("grpId", groupId);
		i.putExtra("grpName", groupName);
		startActivityForResult(i, ADD_CONTACT);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		refreshList();
	}
	protected void deleteGroup(int position){
		int groupId=groupsList.get(position).getId();
		ga.open();
		ga.deleteGroup(groupId);
		ga.close();
		refreshList();
	}
	private void refreshList(){
		ga.open();
		groupsList=ga.fetchGroups();
		lv.setAdapter(new GroupListAdapter(getApplicationContext(),groupsList));
		ga.close();
	}
}